#!/usr/bin/env python3
import sys
from scapy.all import Ether, IP, TCP, UDP, sendp, get_if_hwaddr, get_if_list

def get_eth0():
    for iface in get_if_list():
        if "eth0" in iface:
            return iface
    print("Error: eth0 not found")
    sys.exit(1)

def main():
    if len(sys.argv) != 5:
        print("Usage: ./send.py <src IP> <dst IP> <protocol(tcp/udp)> <dst port>")
        print("  h2: 10.0.1.2 ")
        print("  h3: 10.0.1.3 ")
        sys.exit(1)

    src_ip = sys.argv[1]
    dst_ip = sys.argv[2]
    proto = sys.argv[3].lower()
    dport = int(sys.argv[4])
    iface = get_eth0()

    pkt = Ether(
        src=get_if_hwaddr(iface),
        dst="ff:ff:ff:ff:ff:ff"
    ) / IP(
        src=src_ip,
        dst=dst_ip,
        ttl=64
    )

    if proto == "tcp":
        pkt /= TCP(sport=6666, dport=dport) / f"TCP test (port {dport})"
    elif proto == "udp":
        pkt /= UDP(sport=6666, dport=dport) / f"UDP test (port {dport})"
    else:
        print("Error: Protocol must be tcp or udp")
        sys.exit(1)

    sendp(pkt, iface=iface, verbose=True)
    print(f"Sent {proto.upper()} packet: {src_ip}:6666 → {dst_ip}:{dport}")

if __name__ == '__main__':
    main()
